package org.example.backend.Module.School.Course.Service.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.example.backend.Module.School.Course.Mapper.CourseGradeMapper;
import org.example.backend.Module.School.Course.Pojo.Entity.CourseGrade;
import org.example.backend.Module.School.Course.Service.CourseGradeService;
import org.example.backend.Module.System.User.Pojo.Entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CourseGradeServiceImpl implements CourseGradeService {

    @Resource
    private CourseGradeMapper courseGradeMapper;

    @Override
    public List<CourseGrade> list(String name) {
        LambdaQueryWrapper<CourseGrade> qw = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)) {
            qw.like(CourseGrade::getName, name);
        }

        return courseGradeMapper.selectList(qw);
    }

    @Override
    public void addOrUpdate(CourseGrade grade, User creator) {
        Date now = new Date();

        grade.setUpdateTime(now);

        if(grade.getId() == null) {
            grade.setCreator(creator.getId());
            grade.setCreateTime(now);
        }

        courseGradeMapper.insertOrUpdate(grade);
    }

    @Override
    public void delete(Long id) {
        courseGradeMapper.deleteById(id);
    }
}
